/**
 * 埋点上传内容 MongoDB Schema
 */

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type TrackingUploadDocument = TrackingUpload & Document;

@Schema({ collection: 'tracking_uploads', timestamps: true })
export class TrackingUpload {
  @Prop({ required: true, index: true })
  trackingId: string;

  @Prop({ required: true })
  websiteId: number;

  @Prop({ required: true })
  uploadType: string; // 'event', 'error', 'performance', 'custom'

  @Prop({ required: true, type: Object })
  data: Record<string, any>;

  @Prop({ required: true, index: true })
  ip: string;

  @Prop()
  userAgent?: string;

  @Prop({ index: true })
  sessionId?: string;

  @Prop({ default: Date.now, index: true })
  timestamp: Date;

  @Prop({ type: Object })
  metadata?: {
    userId?: string;
    pageUrl?: string;
    referrer?: string;
    [key: string]: any;
  };
}

export const TrackingUploadSchema = SchemaFactory.createForClass(TrackingUpload);

// 创建索引
TrackingUploadSchema.index({ trackingId: 1, timestamp: -1 });
TrackingUploadSchema.index({ websiteId: 1, uploadType: 1, timestamp: -1 });
TrackingUploadSchema.index({ sessionId: 1 });
TrackingUploadSchema.index({ uploadType: 1, timestamp: -1 });

